<?php

namespace Admin\Controller;
use Admin\Model\AuthGroupModel;
use Think\Page;

/**
 * 评论控制器
 * Class CommentController
 * @package Admin\Controller
 */
class CommentController extends AdminController {

    /**
     * 删除评论
     * @author leiying
     */
    public function delComment(){
        if(IS_POST){
            $id = I("post.id");
            if($id){
                $res = M('goods_comment')->where(array('id'=>I('post.id')))->delete();
                if($res){
                    $this->success('删除成功');
                }else{
                    $this->error('删除失败');
                }
            }else{
                $this->error("请选择要删除的数据");
            }
        }else{
            $this->error('非法操作');
        }
    }

    /**
     * 修改评论审核状态
     * @author leiying
     */
    public function updateCheck(){
        if(IS_POST){
            $data["is_check"] = intval($_POST["status"]);
            $map["id"] = intval($_POST["id"]);
            $result = M('goods_comment')->where($map)->save($data);
            if(is_numeric($result)){
                $this->success("执行成功");
            }else{
                $this->error("执行失败");
            }
        }else{
            $this->error('非法操作');
        }
    }

    /**查询回复的评论
     * @author leiying
     */
    public function findChildComment(){
        if(I('post.btn')){$this->success('');}
        $id = I('post.id');

        $child = M("goods_comment")->alias("a")
            ->field("a.id comment_id,a.dialog_id,a.is_check,e.nickname p_nickname,e.user_id p_uid,b.nickname,b.headimg,b.user_id uid,b.user_phone,a.replytime,a.star,a.pid p_comment_id,a.content,a.img_url1,a.img_url2,a.img_url3,a.agreecount,c.goods_attr")
            ->join("km_users b ON a.uid=b.user_id","left")
            ->join("km_order_goods c ON a.order_id=c.order_id","left")
            ->join("km_goods_comment d ON a.pid=d.id","left")
            ->join("km_users e ON d.uid=e.user_id","left")
            ->where(array("a.dialog_id"=>$id,"a.goods_id"=>array("exp"," = c.goods_id"),"a.goods_type"=>array("exp"," = c.goods_type")))->select();
        foreach($child as &$v){
            $v['headimg'] = !empty($v["headimg"])?$v["headimg"]:"/Uploads/default_headimg.png";
            $v["replytime"] = date("Y-m-d H:i:s",$v["replytime"]);
            $v["nickname"] = !empty($v["nickname"])?$v["nickname"]:'';
            $v["p_nickname"] = !empty($v["p_nickname"])?$v["p_nickname"]:'';
        }
        if($child !== false){
            $child = $child?$child:array();
            $tree = list_to_tree($child,"comment_id","p_comment_id", $child = '_child', $root = $id);
            $array = tree_to_array($tree);

            $this->success($array);
        }else{
            $this->error(M()->getLastSql());
            $this->error("查询异常");
        }
    }


    /**
     * 宝贝评论
     * @author leiying
     */
    public function pCommentList(){
        $limit = LIMIT;
        $get = I('post.');

        $search_data = array();
        $where = array();


        $where['a.pid'] = array('eq',0);//默认显示顶级评论

        if(IS_POST){
            session('searchdata_pCommentList',$get);
        }else{
            if(I('get.p')){
                $this->assign('p',I('get.p'));
            }else{
                session('searchdata_pCommentList',null);
            }
            $get = $search_data = session('searchdata_pCommentList');
        }

        if(isset($get['goods_id']) && $get['goods_id'] != '') {
            $search_data['goods_id'] = $get['goods_id'];
            $where['a.goods_id'] = array('like','%'.$get['goods_id']."%" );
        }

        if(isset($get['goods_type']) && $get['goods_type'] != 'all') {
            $search_data['goods_type'] = $get['goods_type'];
            $where['a.goods_type'] = array('eq',$get['goods_type'] );
        }

        if(isset($get['is_check']) && $get['is_check'] != 'all') {
            $search_data['is_check'] = $get['is_check'];
            $where['a.is_check'] = array('eq',$get['is_check'] );
        }else{
            if($get['is_check'] == 'all'){
                $search_data['is_check'] = $get['is_check'];
                unset($where['a.is_check']);
            }else{
                $where['a.is_check'] = array('eq',2);//默认显示未审核
                $search_data['is_check'] = 2;
            }
        }
        if(isset($get['star']) && $get['star'] != 'all') {
            $search_data['star'] = $get['star'];
            $where['a.star'] = array('eq',$get['star'] );
        }

        $count = M('goods_comment')->alias('a')->where($where)->count();
        $Page = new  \Think\Page($count, $limit);// 实例化分页类 传入总记录数

        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出


        $lists = M('goods_comment')->alias('a')
            ->field('a.*,b.nickname,b.user_phone')
            ->join('km_users b on a.uid = b.user_id','left')
//            ->join('km_goods c on a.goods_id = c.goods_id','left')
            ->where($where)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order("a.addtime desc")->select();
        foreach($lists as &$v){
            $v["reply_count"] = M("goods_comment")->where(array("dialog_id"=>$v["id"]))->count();

            switch($v["goods_type"]){
                case 'A':
                    $table = "goods";
                    $id_field = 'goods_id';
                    $goods_name = 'goods_name';
                    break;
                case 'B':
                    $table = "exchange_goods";
                    $id_field = 'goods_id';
                    $goods_name = 'goods_name';
                    break;
                case 'C':
                    $table = "hot_goods";
                    $id_field = 'goods_id';
                    $goods_name = 'goods_name';
                    break;
                case 'D':
                    $table = "pintuan_goods";
                    $id_field = 'pt_id';
                    $goods_name = 'pt_goods_name';
                    break;
            }
            $v["goods_name"] = M($table)->where(array($id_field=>$v["goods_id"]))->getField($goods_name);
        }

        $this->assign('lists',$lists);
        $this->assign('searchs',$search_data);
        $p = I('get.p',1);
        $this->assign('p',$p);
        $this->display();
    }


}
